package org.cttic.visualization.visualization.z_service.impl;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mongodb.BasicDBObject;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.model.Filters;
import org.bson.Document;
import org.cttic.visualization.visualization.common.commonConf;
import org.cttic.visualization.visualization.common.module.OnlineCData;
import org.cttic.visualization.visualization.common.module.provinceData;
import org.cttic.visualization.visualization.common.retSectionData;
import org.cttic.visualization.visualization.common.sectionBillQuery;
import org.cttic.visualization.visualization.util.DateUtil;
import org.cttic.visualization.visualization.util.ParseJsonDataUtil;
import org.cttic.visualization.visualization.util.sixUtil.OnlineCQueryUtil;
import org.cttic.visualization.visualization.z_service.EmployeeRepository;
import org.cttic.visualization.visualization.z_service.OnlineCQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author shkstart
 * @create 2020-05-13 9:34
 * 2.4.1各省在线车辆总车辆数统计
 */
@Service
public class OnlineCQueryImpl implements OnlineCQuery {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private EmployeeRepository employeeRepository;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private OnlineCQueryUtil onlineCQueryUtil;
    @Override
    public String onlineCQuery(String inputData) {
        JSONObject jsonData = ParseJsonDataUtil.getJsonData(inputData);
        String provinceId = jsonData.getString("provinceId");
        String roadSectionId = jsonData.getString("roadSectionId");//路段ID
        String queryType = jsonData.getString("queryType");
        String queryValue1 = jsonData.getString("queryValue1");
        OnlineCData onlineCData = new OnlineCData();
        List<OnlineCData.vehArray> list = new ArrayList<>();
        //按全国查询
        long totalOnline=0L;
        long total=0L;
        if (provinceId==null){

            //按客户端请求时间段查询  第一版查当日
            if(queryType.equals("1")){
                String queryValue2 = jsonData.getString("queryValue2");
                String startTime= DateUtil.TtoTime(queryValue1);
                String endTime = DateUtil.TtoTime(queryValue2);

                //调用统计接口查询各省份信息
                for (Integer province : provinceData.provinceList) {
                    //查询
                    System.out.println("***************开始检索mysql表*****************");
                    String value = onlineCQueryUtil.queryUtil(startTime, endTime, province, list);
                    if(value.contains("|")){
                        String[] split = value.split("\\|");
                        totalOnline += Long.parseLong(split[0]);
                        total += Long.parseLong(split[1]);
                    }
                    System.out.println("***************完成检索mysql表*****************");
                }
                onlineCData.setVehArray(list);
                onlineCData.setTotalRegCounts(total+"");
                onlineCData.setTotalCounts(totalOnline+"");
                ObjectMapper mapper = new ObjectMapper();
                try {
                    String value = mapper.writeValueAsString(onlineCData);
                    return value;
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }
                //按系统当前时间点查询
            }else if (queryType.equals("0")){

            }

            //单省查询
        }else if(!provinceId.contains("|")){
            //时间段查询
            if(queryType.equals("1")){
                String queryValue2 = jsonData.getString("queryValue2");
                String startTime= DateUtil.TtoTime(queryValue1);
                String endTime = DateUtil.TtoTime(queryValue2);

                String count = onlineCQueryUtil.queryUtil(startTime, endTime, Integer.parseInt(provinceId), list);
                if(count.contains("|")){
                    String[] split = count.split("\\|");
                    totalOnline += Long.parseLong(split[0]);
                    total += Long.parseLong(split[1]);
                }
                onlineCData.setVehArray(list);
                onlineCData.setTotalRegCounts(total+"");
                onlineCData.setTotalCounts(totalOnline+"");
                ObjectMapper mapper = new ObjectMapper();
                try {
                    String value = mapper.writeValueAsString(onlineCData);
                    return value;
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }

            }else {

            }

            //多省查询
        }if(provinceId.contains("|")){
            if(queryType.equals("1")){
                String queryValue2 = jsonData.getString("queryValue2");
                String startTime= DateUtil.TtoTime(queryValue1);
                String endTime = DateUtil.TtoTime(queryValue2);
                String[] split = provinceId.split("\\|");

                if(split!=null&&split.length>0){
                    for (String province : split) {
                        String count = onlineCQueryUtil.queryUtil(startTime, endTime,Integer.parseInt(province), list);
                        if(count.contains("|")){
                            String[] countSplit = count.split("\\|");
                            totalOnline += Long.parseLong(countSplit[0]);
                            total += Long.parseLong(countSplit[1]);
                        }
                    }
                    onlineCData.setVehArray(list);
                    onlineCData.setTotalRegCounts(total+"");
                    onlineCData.setTotalCounts(totalOnline+"");
                    ObjectMapper mapper = new ObjectMapper();
                    try {
                        String value = mapper.writeValueAsString(onlineCData);
                        return value;
                    } catch (JsonProcessingException e) {
                        e.printStackTrace();
                    }
                }
            }else {

            }
        }




        return null;
    }
}
